package com.clerkq.CircleList.view
{
	import com.clerkq.CircleList.data.ThumbVVO;
	import com.clerkq.CircleList.event.CircleListEvent;
	import com.content.model.Collection.ItemGroup;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.Geometry;
	
	import flash.display.Shape;
	import flash.display.Sprite;
	
	public class ThumbListV extends UIObject implements IDisposable
	{
		// Display number
		private var _visibleNum:uint;
		private var _activeThumbIndex:int;
		// Total number for how many buttons should be placed on the left/right side
		private var _leftNum:uint;
		private var _rightNum:uint;
		// 显示区域内(mask内)最左和最右的thumb的seq号(不是index号)
		private var _topBoundSeq:int;
		private var _bottomBoundSeq:int;
		// Top/Bottom extreme position for button gallery
		private var _topLimPos:Number;
		private var _bottomLimPos:Number;
		
		public var top:Number = 0;
		public var bottom:Number = 0;
		private var _thumbPipe:Array;
		
		private var _thumbSpace:int;
		private var _thumbHeight:int;
		
		public function ThumbListV(visibleNum:int, thumbSpace:int, thumbHeight:int)
		{
			super();
			_thumbSpace = thumbSpace;
			_thumbHeight = thumbHeight;
			
			_visibleNum = visibleNum;
			_thumbPipe = new Array();
		}
		
		public function MoveList(distance:Number = 0):void{
			var direct:String;
			if(distance >= 0){
				direct = "top";
        	}else if(distance < 0){
				direct = "bottom";
        	}
            	
			var thumbNum:uint = _thumbPipe.length;
			if(direct == "top"){
				
				var temp:Number = (thumbNum - _visibleNum)/2.0;
 				var temp2:Number = Math.floor((thumbNum - _visibleNum)/2.0);
				_leftNum = temp < 0 ? 0:temp2;
				// If the total number is even, the rightNumber should be larger than the leftNumber.
				_topLimPos = _thumbSpace * _leftNum * (-1);			
				
				// Total thumbNails number is Even
				if( !Boolean(thumbNum % 2) ){
					// Odd
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum;
					}else{
						_rightNum = _leftNum + 1;					
					}				
				}else{
					// Even
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum + 1;
					}else{
						_rightNum = _leftNum;
					}
				}			
			}else if(direct == "bottom"){
				
				var temp3:Number = Math.ceil((thumbNum - _visibleNum)/2.0);
 				var temp4:Number = 0;
				_leftNum = temp3 < 0 ? temp4:temp3;
				
				// If the total number is even, the rightNumber should be larger than the leftNumber.
				_topLimPos = _thumbSpace * (_leftNum ) * (-1);			
				
				if( !Boolean(thumbNum % 2) ){
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum;
					}else{
						_rightNum = _leftNum - 1;					
					}				
				}else{
					if(!Boolean(_visibleNum % 2)){
						_rightNum = _leftNum - 1;
					}else{
						_rightNum = _leftNum;
					}
				}			
			}
			_bottomLimPos = _thumbSpace * (_rightNum + _visibleNum );	
			
			// local variable
			var i:uint;						
			
			// 1. caculate	( 这里算出 GroupTop/GroupBottom 是为了判断移动后会不会出界)		
			var groupTop:Number;			
			var groupBottom:Number;
			groupTop = (_thumbPipe[0].y - _thumbHeight/2) + distance;
			groupBottom = groupTop + _thumbSpace * (thumbNum);
			
			// Number of buttons that over bound
			var overNumber:Number = 0;			
			
			var headThumbY:Number = _thumbPipe[0].y;//(_thumbs[0].y + Thumb.HEIGHT/2);
			if(direct == "top"){
				if( groupBottom >= _bottomLimPos ){
					// over right				
					overNumber = Math.ceil((groupBottom - _bottomLimPos)/_thumbSpace);
					//trace("Over Right Number is : "+ overNumber);
					for( i = 0; i < overNumber; i++ ){
						var tempBottom:ThumbV = _thumbPipe.pop();
						_thumbPipe.unshift(tempBottom);
					}				
				}
			}
			if(direct == "bottom"){
				if( groupTop < _topLimPos ){
					// over top
					overNumber = Math.ceil((_topLimPos - groupTop)/_thumbSpace);
					for( i = 0; i < overNumber; i++ ){
						var tempTop:ThumbV = _thumbPipe.shift();
						_thumbPipe.push(tempTop);
					}
				}
			}
			
			// 4. Redraw all buttons
			var insertPoint:Number;
			var offset:Number;
			var offsetBack:Number;
			
			if(direct == "top"){
				offsetBack = overNumber * ( - _thumbSpace);
			}else if(direct == "bottom"){
				offsetBack = overNumber * (_thumbSpace);
			}	
					
			offset = distance;
			insertPoint = Number(headThumbY + offset + offsetBack);
			
			// If there is any thumb overlaped, array is changed, so have to refresh the list
			if(overNumber != 0){				
				for( i = 0; i < thumbNum; i++ ){
					removeChild(_thumbPipe[i]);
				}
				for( i = 0; i<thumbNum; i++ ){
					addChild(_thumbPipe[i]);
				}
			}
			
			// Caculate top/bottom inner thumb seq
			top = 0;
			bottom = _thumbSpace*_visibleNum - (_thumbSpace - _thumbHeight);
			
			// Move elements in list
			for( i = 0; i<thumbNum; i++ ){
				_thumbPipe[i].y = insertPoint;
				insertPoint += _thumbSpace;
//				size = Math.abs(bottom/2 - _thumbs[i].y)/1000;
//				Thumb(_thumbs[i]).size = size;
			}
			
			for( i = 0; i < thumbNum; i++ ){
				if((_thumbPipe[i].y - _thumbHeight/2) >= top ){
					_topBoundSeq = i;
					break;
				};
			}
			var thumbBottom:Number;
			for( i = 0; i < thumbNum; i++ ){
				thumbBottom = _thumbPipe[i].y + _thumbHeight/2
				if( thumbBottom > bottom ){
					_bottomBoundSeq = i-1;
					break;
				};
			}
//			trace("_topBoundSeq = " + _topBoundSeq + "    " + "_bottomBoundSeq = " + _bottomBoundSeq);
			
			// 因为这里的变化都是一步到位，所以没有Tween;之所以要回移，就是为了在父级里面用Tween
			this.y += distance * (-1);
			
			// 5. Tell the others (目前而言，在上一层的CircleList中并未处理 Update)
//			dispatchEvent(new CircleListEvent(CircleListEvent.UPDATE_COMPLETE));
		}
		
		private function ClearThumb():void{
			var delThumb:ThumbV;
			while(_thumbPipe.length > 0){
				delThumb = _thumbPipe.pop();
				delThumb.Dispose();
			}
			_thumbPipe.length = 0;
		}
		
		public function FillList(itemList:ItemGroup):void{
			ClearThumb();
			
			_topBoundSeq = 0;			
			var insertPosY:Number = 0 + _thumbHeight/2;
			var i:uint;
			var len:uint = itemList.Length;			
			for( i = 0; i<len; i++ ){
				var thumbVO:ThumbVVO = itemList.Items[i] as ThumbVVO;
				var thumb:ThumbV = new ThumbV(thumbVO);
				_thumbPipe.push(thumb);
				addChild(thumb);
				thumb.y = insertPosY;
				insertPosY += _thumbSpace;
			}
			
			// Relocate position
			MoveList(0);
		}
		
		// GET AND SET /////////////////
		public function get topBoundSeq():int		{		return _topBoundSeq;		}
		public function get bottomBoundSeq():int	{		return _bottomBoundSeq;		}
		public function get thumbPipe():Array		{		return _thumbPipe;			}
		
		public function Dispose():void				{		ClearThumb();				}
	}
}